package Persistencia;

import Entidades.EntContrato;
import java.sql.Connection;
//import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author maxtor
 */
public class DaoContrato {

    public List<EntContrato> ListarContrato(Connection con) {
        //usuario,clave, perfil, estado
        List<EntContrato> resul = new ArrayList<EntContrato>();
        //    c.setId("no existe");
        try {
            PreparedStatement p = con.prepareStatement(ContratoSqlHelper.getAllContrato());
           // System.out.println(ArrendatarioSqlHelper.getAllArrendatario());
            ResultSet r = p.executeQuery();
            while (r.next()) {
                EntContrato c = new EntContrato();
                c.setCedulaArrendatario(r.getString("CedulaArrendatario"));
                c.setCodigoInmueble(r.getString("CodigoInmueble"));
                c.setCodigoContratodeA(r.getString("CodigoContratodeA"));
                c.setValorMensual(r.getString("ValorMensual"));
                c.setFechadePago(r.getString("FechadePago"));
                c.setPermanenciaMinima(r.getString("PermanenciaMinima"));

                resul.add(c);

            }
        } catch (Exception e) {
            e.printStackTrace();
            //    return c;

        } finally {
            try {
                con.close();
            } catch (Exception ex) {
            }

        }
        return resul;
    }

    public int crear(Connection c, EntContrato Contrato) {
        String sql = ContratoSqlHelper.crear();
        int resultadoCrear = 0;
        try {
            PreparedStatement st = c.prepareStatement(sql);

            st.setString(1, Contrato.getCedulaArrendatario());
            st.setString(2, Contrato.getCodigoInmueble());
            st.setString(3, Contrato.getCodigoContratodeA());
            st.setString(4, Contrato.getValorMensual());
            st.setString(5, Contrato.getFechadePago());
            st.setString(6, Contrato.getPermanenciaMinima());



            resultadoCrear = st.executeUpdate();

        } catch (SQLException ex) {
            Logger.getLogger(DaoArrendatario.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                c.close();
            } catch (SQLException ex) {
                Logger.getLogger(DaoContrato.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return resultadoCrear;
    }

     public int modificar(Connection c, EntContrato Contrato) {
     String sql = ContratoSqlHelper.modificar();
     int resultadoModificar = 0;
     try {
     PreparedStatement st = c.prepareStatement(sql);
            
     st.setString(1, Contrato.getValorMensual());
     st.setString(2, Contrato.getFechadePago());
     st.setString(3, Contrato.getPermanenciaMinima());
     st.setString(4, Contrato.getCedulaArrendatario());
     st.setString(5, Contrato.getCodigoInmueble());
     st.setString(6, Contrato.getCodigoContratodeA());


     resultadoModificar = st.executeUpdate();

     } catch (SQLException ex) {
     Logger.getLogger(DaoContrato.class.getName()).log(Level.SEVERE, null, ex);
     } finally {
     try {
     c.close();
     } catch (SQLException ex) {
     Logger.getLogger(DaoContrato.class.getName()).log(Level.SEVERE, null, ex);
     }
     }

     return resultadoModificar;
     }

     public EntContrato Buscar(Connection con,String CodigoContratodeA) {
       EntContrato c = new EntContrato();
       // c.setNroDocumento("Este cliente no existe");
        try {
            PreparedStatement cl = con.prepareStatement(ContratoSqlHelper.Buscar(CodigoContratodeA));
            ResultSet r = cl.executeQuery();
            while (r.next()) {
               c.setCedulaArrendatario(r.getString(1));
               c.setCodigoInmueble(r.getString(2));
               c.setCodigoContratodeA(r.getString(3));
               c.setValorMensual(r.getString(4));
               c.setFechadePago(r.getString(5));
               c.setPermanenciaMinima(r.getString(6));

               
            }
        } catch (Exception e) {
            e.printStackTrace();
            return c;

        } finally {
            try {
                con.close();
            } catch (Exception ex) {
            }

        }
        return c;
    }
     public int eliminar(Connection c, EntContrato Contrato) {
     String sql = ContratoSqlHelper.eliminar();
     int resultadoEliminar = 0;
     try {
     PreparedStatement st = c.prepareStatement(sql);

     st.setString(1, Contrato.getCodigoContratodeA());

     resultadoEliminar = st.executeUpdate();

     } catch (SQLException ex) {
     Logger.getLogger(DaoArrendatario.class.getName()).log(Level.SEVERE, null, ex);

     } finally {
     try {
     c.close();
     } catch (SQLException ex) {
     Logger.getLogger(DaoArrendatario.class.getName()).log(Level.SEVERE, null, ex);
     }
     }

     return resultadoEliminar;
     }
}
